{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-08T11:04:31.857009Z",
     "start_time": "2020-04-08T11:04:31.839056Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import docx\n",
    "from docx import Document\n",
    "from docx.shared import Inches\n",
    "from docx.enum.text import WD_ALIGN_PARAGRAPH"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def profileGenerate(user):\n",
    "\n",
    "    document = Document()                          # 打开一个基于默认“模板”的空白文档\n",
    "    document.add_heading('个人简历', 0)      # 添加标题\n",
    "\n",
    "    attrs=[\n",
    "        ['姓名',user.name,'性别',user.sex],\n",
    "        ['年龄',user.age,'教育水平',user.edu],\n",
    "        ['邮箱',user.email,'手机号',user.phone],\n",
    "        ['博客地址',user.blog,'工作年龄',user.workingYear],\n",
    "        ['期望薪资',user.salaryWanted,'工作地址',user.address]\n",
    "    ]\n",
    "\n",
    "    table = document.add_table(rows=1, cols=4) # 添加表\n",
    "\n",
    "    flag=1\n",
    "    for l1,v1,l2,v2 in attrs:\n",
    "        if (flag):\n",
    "            cells = table.rows[0].cells\n",
    "            flag =0\n",
    "        else:\n",
    "            cells = table.add_row().cells\n",
    "        cells[0].text = l1\n",
    "        cells[1].text = v1\n",
    "        cells[2].text = l2\n",
    "        cells[3].text = v2\n",
    "\n",
    "\n",
    "    document.add_heading('工作经验', level=1)\n",
    "    document.add_paragraph('\\t'+user.exp) \n",
    "\n",
    "    document.add_heading('获得荣誉', level=1)\n",
    "    document.add_paragraph('\\t'+user.glory)\n",
    "\n",
    "    document.add_heading('个人经历', level=1)\n",
    "    document.add_paragraph('\\t'+user.description)\n",
    "\n",
    "\n",
    "    abilityTable = document.add_table(rows=3, cols=2)\n",
    "    attrs2 = [\n",
    "        ['专业能力',user.professionSkill],\n",
    "        ['个人能力',user.personalSkill],\n",
    "        ['工具使用',user.toolSkill]\n",
    "    ]\n",
    "\n",
    "    for i in range(len(abilityTable.rows)):\n",
    "        cells = abilityTable.rows[i].cells\n",
    "        cells[0].text=attrs2[i][0]\n",
    "        celss[1].text=attrs2[i][1]\n",
    "\n",
    "        \n",
    "    return document  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-08T11:16:16.589091Z",
     "start_time": "2020-04-08T11:16:16.419290Z"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "document = Document()                          # 打开一个基于默认“模板”的空白文档\n",
    "document.add_heading('个人简历', 0)      # 添加标题\n",
    "\n",
    "\n",
    "document.add_picture('timg.jpg')\n",
    "p = document.add_paragraph('A plain paragraph having some ')    # 增加一段\n",
    "p.add_run('bold').bold = True                                   # 设置样式\n",
    "p.add_run(' and some ')\n",
    "p.add_run('italic.').italic = True\n",
    "\n",
    "\n",
    "p = document.add_paragraph(\"!!!!!!!!!!!\")\n",
    "p_format = p.paragraph_format\n",
    "p_format.left_indent = Inches(20)\n",
    "\n",
    "\n",
    "attrs=[\n",
    "    ['姓名','性别'],\n",
    "    ['年龄','教育水平'],\n",
    "    ['邮箱','手机号'],\n",
    "    ['博客地址','工作年龄'],\n",
    "    ['期望薪资','工作地址']\n",
    "]\n",
    "\n",
    "table = document.add_table(rows=1, cols=4) # 添加表\n",
    "\n",
    "flag=1\n",
    "for l1,l2 in attrs:\n",
    "    if (flag):\n",
    "        cells = table.rows[0].cells\n",
    "        flag =0\n",
    "    else:\n",
    "        cells = table.add_row().cells\n",
    "    cells[0].text = l1\n",
    "    cells[2].text = l2\n",
    "# hdr_cells = table.rows[0].cells            # 设置表首行标题\n",
    "# hdr_cells[0].text = '姓名'                  # 表首行标题赋值\n",
    "# hdr_cells[1].text = '年龄'\n",
    "# hdr_cells[2].text = '性别'\n",
    "\n",
    "# for i in rang(len(table.rows)):\n",
    "#     cells = table.rows[i].cells\n",
    "#     cells[0] = attrs[i]\n",
    "\n",
    "# for qty, id, desc in records:              # for循环将records内容赋值到单元格内\n",
    "#     row_cells = table.add_row().cells\n",
    "#     row_cells[0].text = str(qty)\n",
    "#     row_cells[1].text = id\n",
    "#     row_cells[2].text = desc\n",
    "\n",
    "\n",
    "document.add_heading('工作经验', level=1)\n",
    "# document.add_paragraph('当过街头混混', style='Intense Quote')\n",
    "document.add_paragraph('\\t'+'A plain paragraph having some ') \n",
    "\n",
    "document.add_heading('获得荣誉', level=1)\n",
    "document.add_paragraph('第一打手', style='Intense Quote')\n",
    "\n",
    "document.add_heading('个人经历', level=1)\n",
    "document.add_paragraph('做过5年牢', style='Intense Quote')\n",
    "    \n",
    "\n",
    "abilityTable = document.add_table(rows=3, cols=2)\n",
    "attrs2 = ['专业能力','个人能力','工具使用']\n",
    "\n",
    "for i in range(len(abilityTable.rows)):\n",
    "    cells = abilityTable.rows[i].cells\n",
    "    cells[0].text=attrs2[i]\n",
    "\n",
    "# cells = table.rows[0].cells\n",
    "    \n",
    "# document.add_page_break()                 # 添加分页符\n",
    "\n",
    "document.save('./demo.docx')              # 保存文件     \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
